<%- include('../layout/header.ejs') %>

  <link rel="stylesheet" href="/node_modules/mditor/dist/css/mditor.min.css">
  <script src="/node_modules/mditor/dist/js/mditor.min.js"></script>

  <div class="container">
    <h1>编辑文章页</h1>
    <hr>
    <form id="form">
      <!--应该把文章的标题，作为 隐藏域，保存到 表单中-->
      <input type="hidden" name="id" value="<%= article.id %>">
      <div class="form-group">
        <label>文章标题：</label>
        <input type="text" name="title" class="form-control" required value="<%= article.title %>">
      </div>

      <div class="form-group">
        <label>文章内容：</label>
        <textarea name="content" class="form-control" id="editor"><%= article.content %></textarea>
      </div>

      <div class="form-group">
        <input type="submit" value="保存文章" class="btn btn-primary">
      </div>
    </form>
  </div>

  <script>
//  let mditor=mditor.from
 let mditor=Mditor.fromTextarea(document.getElementById("editor"))
//  点击保存文章 把数据发送到后台去 去数据库修改
$("#form").on("submit",function(e){
    e.preventDefault()
    $.ajax({
      type:"post",
      url:"/article/edit",
      data:$("#form").serialize(),
      success:function(res){
        console.log(res)
        // 如果添加成功 拿到id 跳转到当前文章去
        if(res.status==200){
          alert("添加文章成功")
          // location.href="/article/info/"+res.insertId
          location.href="/article/info/<%=article.id%>"

        }
        
      }
    })
  })
  </script>

  <%- include('../layout/footer.ejs') %>